package com.xsedge.xsedge.service.impl;/*
 * @Author ljc
 * @Description //TODO $
 * @Param $
 * @return $
 **/

import com.xsedge.xsedge.dao.*;
import com.xsedge.xsedge.entity.*;
import com.xsedge.xsedge.service.RunDockerApiService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import static com.xsedge.xsedge.tools.SSHTools.sshConnection;

@Service
public class RunDockerApiServiceImp implements RunDockerApiService {
	@Autowired
	private ContainersRepository containersRepository;

	@Autowired
	private NodeRepository nodeRepository;

	@Autowired
	private NodeLogRepository nodeLogRepository;

	@Autowired
	private CallFreqRepository callFreqRepository;

	@Autowired
	private ImageRepository imageRepository;

	//传入参数！
	@Override
	public Map<String, Object> runDocker(String nodeIp, String imageName) throws IOException {

		String cmds;

		//先查arch
		NodeInfo nodeInfo = nodeRepository.findNodeByIp(nodeIp);
		String node_name = nodeInfo.getnodeName();
		String arch = nodeInfo.getnodeArch();

		//docker run -d -p mapping_port:container_port image_name:1.0
		//NodeLog nodeLog = nodeLogRepository.findNodeLogByNodeIp(nodeIp);
		//从端口8100开始，统计当前节点的pod数，8100加pod数，即为当前mapping_port
		Object o = containersRepository.countByNodeIp(nodeIp);
		int count_pod  = 0;
		if (o!=null){
			count_pod = Integer.parseInt(o.toString());
		}

		//对外映射端口
		int mapping_port = 8100 + count_pod;

		cmds = "docker run -d -p "+ mapping_port + ":"+ 8080
				+ " " + imageName + ":1.0";
		//查找内网穿透的服务器ip和内网穿透的映射port
		String ssh_mapping_ip = nodeInfo.getipMapping();
		int ssh_mapping_port = nodeInfo.getportMapping();
		//sshConnection("101.201.155.125", 6000, "xsedge", false, cmds);
		//ssh 免密登录执行
		String ssh_res = sshConnection(ssh_mapping_ip, ssh_mapping_port, "xsedge", false, cmds);

		String result;
		String container_id;
		if (ssh_res.contains("invalid"))
		{
			result = "error";
			container_id="failed";
		}else {
			Images images = imageRepository.findByImageName(imageName);
			String service_name = images.getServiceName();
			//返回containerId前12位
			String containerId=ssh_res.substring(0,12);
			//插入数据库
			Container containers = new Container();
			containers.setContainerId(containerId);
			containers.setContainerPort(8080);
			containers.setMappingPort(mapping_port);
			containers.setServiceName(service_name);
			containers.setImageName(imageName);
			containers.setNodeIp(nodeIp);
			containers.setNodeName(node_name);
			insertContainerLog(containers);
			result = "success";
			container_id = containerId;

			CallFreq callFreq = new CallFreq();
			callFreq.setContainerId(container_id);
			callFreq.setCounts(0);
			callFreq.setServiceName(service_name);
			callFreq.setNodeIp(nodeIp);
			callFreq.setNodeName(node_name);
			callFreqRepository.save(callFreq);

		}

		Map<String,Object> map = new HashMap<>();
		map.put("result",result);
		map.put("containerId",container_id);
		return map;
	}

	private void insertContainerLog(Container container) {
		containersRepository.save(container);
	}
}